package com.wfs.myapp.service.impl;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.common.Constant;
import com.wfs.myapp.entity.domain.AiTalk;
import com.wfs.myapp.enums.AITalkRoleEnum;
import com.wfs.myapp.enums.ResponseCodeEnum;
import com.wfs.myapp.remote.wx.BaiduRemoteService;
import com.wfs.myapp.remote.wx.dto.TalkAIDto;
import com.wfs.myapp.remote.wx.vo.BaiduTalkAiVo;
import com.wfs.myapp.remote.wx.vo.SendMessageVo;
import com.wfs.myapp.repository.bk.AiTalkRepository;
import com.wfs.myapp.service.AIService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class AIServiceImpl implements AIService {

    @Autowired
    private BaiduRemoteService baiduRemoteService;

    @Autowired
    private AiTalkRepository aiTalkRepository;

    @Override
    public ApiResult<String> talkAI(String message, Long userId) {
        AiTalk checkAiTalk = aiTalkRepository.findLastTimeByUserId(userId);
        String sessionId;
        List<TalkAIDto.TalkAiMessageDto> messageDtoList = new ArrayList<>();
        // db sessionId
        if (Objects.isNull(checkAiTalk) || checkAiTalk.getCreatedTime().plusMinutes(Constant.AI_ONE_SESSION_TIME_OUT_MINUTES).compareTo(LocalDateTime.now()) < 0) {
            // new session
            sessionId = String.valueOf(System.currentTimeMillis());
        } else {
            // old session
            sessionId = checkAiTalk.getSessionId();
            List<AiTalk> sessionMessageList = aiTalkRepository.findBySessionIdOrderByCreatedTime(sessionId);
            for (AiTalk item : sessionMessageList) {
                messageDtoList.add(new TalkAIDto.TalkAiMessageDto(item.getRole(), item.getContent()));
            }
        }
        // db user talk
        AiTalk userTalk = new AiTalk();
        userTalk.setRole(AITalkRoleEnum.USER.getRole());
        userTalk.setContent(message);
        userTalk.setSessionId(sessionId);
        userTalk.setUserId(userId);
        userTalk.setStatus(0);
        userTalk.setCreatedBy(userId.toString());
        userTalk.setUpdatedBy(userId.toString());
        userTalk.setUserId(userId);
        AiTalk save = aiTalkRepository.save(userTalk);
        messageDtoList.add(new TalkAIDto.TalkAiMessageDto(AITalkRoleEnum.USER.getRole(), message));
        // call baidu ai
        BaiduTalkAiVo talkAiVo = baiduRemoteService.talkAI(new TalkAIDto(messageDtoList));
        if (Objects.isNull(talkAiVo) || Objects.nonNull(talkAiVo.getError_code())) {
            return ApiResult.error(ResponseCodeEnum.REMOTE_ERROR);
        }
        String result = talkAiVo.getResult();
        save.setStatus(1);
        aiTalkRepository.save(save);
        // db assistant talk
        AiTalk assistant = new AiTalk();
        assistant.setRole(AITalkRoleEnum.ASSISTANT.getRole());
        assistant.setContent(talkAiVo.getResult());
        assistant.setSessionId(sessionId);
        assistant.setUserId(userId);
        assistant.setStatus(1);
        assistant.setCreatedBy(userId.toString());
        assistant.setUpdatedBy(userId.toString());
        assistant.setUserId(userId);
        aiTalkRepository.save(assistant);
        return ApiResult.success(result);
    }

    @Override
    public ApiResult<List<AiTalk>> sessionHisList(Long userId) {
        return ApiResult.success(aiTalkRepository.findByUserIdAndCreatedTime(userId, LocalDateTime.now().minusMinutes(Constant.AI_ONE_SESSION_TIME_OUT_MINUTES)));
    }


}
